package com.cy.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.ListModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.cy.entity.JsonResult;
import com.cy.entity.Manager;
import com.cy.entity.Meetingroom;
import com.cy.entity.Order_list;
import com.cy.entity.Type;
import com.cy.service.ManagerService;
import com.cy.service.MeetingroomService;
import com.cy.service.Order_listService;
import com.cy.service.TypeService;
import com.cy.service.UploadService;

@RestController
public class MeetingroomController {
	
	@Autowired
	private MeetingroomService service;
	@Autowired
	private TypeService typeService;
	
	@Autowired
	private Order_listService order_listService;
	
	@Autowired
	private UploadService uploadService;
	
	@Autowired
	private ManagerService managerService;
	
	@RequestMapping("showMeetingroomByType")
	public JsonResult showMeetingroomByType() {
		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		List<Type> types = typeService.findAllType();
		for (Type type : types) {
			List<Meetingroom> meetingrooms = service.findMeetingroomByType(type.getTypeid());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("typeId", type.getTypeid());
			map.put("typeName", type.getTypename());
			map.put("rooms", meetingrooms);
			maps.add(map);
		}
		return new JsonResult(maps);
		
	}
	
	@RequestMapping("addMeetroom")
	public JsonResult addMeetroom(@RequestBody MultipartFile file,Integer managerId,
			String name,Integer typeId,Integer num,
			String address,double price,Integer status,String openDate,String closeDate) {
		Meetingroom mt = new Meetingroom();
		JsonResult result = uploadService.uploadImg(file);
		mt.setImage(result.getMessage());
		mt.setAddress(address);
		mt.setManagerid(managerId);
		mt.setName(name);
		mt.setPrice(price);
		mt.setStatus(status);
		mt.setTypeid(typeId);
		mt.setNum(num);
		Date date1 = null;
		Date date2 = null;
		try {
			date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(openDate);
			date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(closeDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mt.setClosedate(date2);
		mt.setOpendate(date1);
		service.addMeetroom(mt);
		return new JsonResult(0,"添加成功");
	}
	
	@RequestMapping("updateMeetroom")
	public JsonResult updateMeetroom(@RequestBody MultipartFile file,Integer managerId,
			String name,Integer typeId,Integer num,
			String address,double price,Integer status,String openDate,String closeDate
			,Integer mid) {
		
		Meetingroom mt = service.findMeetingroomById(mid);
		
		JsonResult result = uploadService.uploadImg(file);
		mt.setImage(result.getMessage());
		mt.setAddress(address);
		mt.setManagerid(managerId);
		mt.setName(name);
		mt.setPrice(price);
		mt.setStatus(status);
		mt.setTypeid(typeId);
		mt.setNum(num);
		Date date1 = null;
		Date date2 = null;
		try {
			date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(openDate);
			date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(closeDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mt.setClosedate(date2);
		mt.setOpendate(date1);
		service.updateMeetroom(mt);
		return new JsonResult(0,"修改成功");
	}
	
	//查询全部会议室
		@RequestMapping("findAllMeetingRooms")
		public JsonResult findAllMeetingRooms() {
			List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
			List<Meetingroom> rooms =  service.findAllMeetingroom();
			if(rooms.size() == 0) {
				return new JsonResult<>(1,"会议室信息为空");
			}
			for (Meetingroom meetingroom : rooms) {
				Map<String, Object> map = new HashMap<String, Object>();
				Type type = typeService.findTypeById(meetingroom.getTypeid());
				map.put("roomInfo", meetingroom);
				map.put("typename", type.getTypename());
				maps.add(map);
			}
			return new JsonResult<>(maps);
		}
		
		//用户查询可预约的会议室
		@RequestMapping("findAllMeetingRoomsByUser")
		public JsonResult findAllMeetingRoomsByUser() {
			
			List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
			List<Meetingroom> rooms =  service.findMeetingRoomsByUser();
			if(rooms.size() == 0) {
				return new JsonResult<>(1,"会议室信息为空");
			}
			for (Meetingroom meetingroom : rooms) {
				Map<String, Object> map = new HashMap<String, Object>();
				Type type = typeService.findTypeById(meetingroom.getTypeid());
				Manager manager = managerService.findManagerById(meetingroom.getManagerid());
				map.put("roomInfo", meetingroom);
				map.put("typename", type.getTypename());
				map.put("manager", manager);
				maps.add(map);
			}
			return new JsonResult<>(maps);
		}
		
		//切换会议室状态
		@RequestMapping("changeRoomStatus")
		public JsonResult changeRoomStatus(int status, int mid) {
			System.out.println(status);
			Meetingroom room = service.findMeetingroomById(mid);
			room.setStatus(status);
			service.updateMeetroom(room);
			return new JsonResult("已经修改状态");
		}


		@RequestMapping("findMeetingroomById")
		public JsonResult findMeetingroomById(Integer mid) {
			Meetingroom mr = service.findMeetingroomById(mid);
			Type type = typeService.findTypeById(mr.getTypeid());
			Manager manager = managerService.findManagerById(mr.getManagerid());
			List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("room", mr);
			map.put("type", type);
			map.put("manager", manager);
			list.add(map);
			return new JsonResult(list);
		}

		
		@RequestMapping("deleteMeetingroom")
		public JsonResult deleteMeetingroom(Integer mid) {
			if(order_listService.findOrder_listByMid(mid).size()>0){
				return new JsonResult<>(0,"有预定单，删除失败");
			}
			service.deleteMeetingroom(mid);
			return new JsonResult<>(0,"success");
		}
		

	//小程序用 通过类型查询
		@RequestMapping("findMeetingroomByTypeId/{typeId}")
		public JsonResult findMeetingroomByTypeId(@PathVariable("typeId") Integer typeId) {
			List<Map<String, Object>> list = new ArrayList<>();
			List<Meetingroom> mrs = service.findMeetingroomByTypeId(typeId);
			for (Meetingroom meetingroom : mrs) {
				
				Map<String, Object> map = new HashMap<>();
				map.put("room", meetingroom);
				Type type = typeService.findTypeById(meetingroom.getTypeid());
				Manager manager = managerService.findManagerById(meetingroom.getManagerid());
				map.put("type", type);
				map.put("manager", manager);
				list.add(map);
			}
			
			return new JsonResult<>(list);
		}
	


}
